/*
 * @Author: liuxiao
 * @Date: 2023-12-12 23:02:59
 * @Last Modified by: liuxiao
 * @Last Modified time: 2023-12-13 22:51:00
 * @课程分类模型
 *
 */

const { DataTypes } = require('sequelize')

const seq = require('../db/seq')

const Category = seq.define('edu_category', {
  title: {
    type: DataTypes.STRING(10),
    allowNull: false,
    comment: '分类名称'
  },
  parent_id: {
    type: DataTypes.CHAR(19),
    allowNull: false,
    defaultValue: '0',
    comment: '父ID,0为父元素'
  },
  level: {
    type: DataTypes.TINYINT,
    allowNull: false,
    defaultValue: 0,
    comment: '当前分类等级，默认为1级分类'
  },
  sort: {
    type: DataTypes.INTEGER(10),
    allowNull: false,
    defaultValue: 0,
    comment: '排序字段'
  }

})

//force:true，强制创建，如果存在表，则删除
//Category.sync({ force: true })

async function createCategory() {
  await Category.sync() // 同步模型和数据库

  let data = [
    {
      title: '前端',
      parent_id: 0
    },
    {
      title: '后端',
      parent_id: 0
    },
    {
      title: 'vue',
      parent_id: 1
    },
    {
      title: 'css',
      parent_id: 1
    },
    {
      title: 'php',
      parent_id: 2
    },
    {
      title: 'spring',
      parent_id: 2
    },
    {
      title: 'pinia',
      parent_id: 3
    },

  ]

  const category = await Category.bulkCreate(data)

 // console.log(category.toJSON())
}

//createCategory().catch(console.error)

module.exports = Category
